$j=jQuery.noConflict();
$j(document).ready(function()
{
	addressSuggest();
	addAddressForm();
});
function keyupInput(input, rusName, max, min, regExp, regExpText)
{
	var valid=true;
	var regExp=new RegExp(regExp);
	var val=$j(input).val();
	if(!regExp.test(val) && val!='')
	{
		var newValue=val.substring(0, val.length - 1);
		$j(input).val(newValue);
		alert('недопустимый символ!('+regExpText+')');
		valid=false;
		return valid;
	}
	if(val.length>max)
	{
		var newValue=val.substring(0, max);
		$j(input).val(newValue);
		alert(rusName+' не может быть длиннее '+max+' символов!');
		valid=false;
		return valid;
	}
	if(val.length<min)
	{
		valid=false;
		return valid;
	}
	return valid;
}
function checkAddress(number)
{
	$j(".streetInput").keyup(function(event)
		{
			var input=$j(this);
			var number=$j(input).attr("id").substr(6);
			if(keyupInput($j("#street"+number), "Улица", 30, 3, "^[-а-яА-ЯёЁ0-9 -.]+$", 'Только русские буквы и цифры и -.') &&
		keyupInput($j("#building"+number), "Дом", 6, 1, "^[-a-zA-Zа-яА-ЯёЁ0-9-\/]+$", 'Только русские и английские буквы и цифры и -/'))
		{
			$j("#checkAddressRes2").html('<img src="main_pics/ok.jpg" width="15" height="15">');
		}
		else $j("#checkAddressRes"+number).html('<img src="main_pics/nok.jpg" width="15" height="15">');
	});
	$j("#building"+number).keyup(function()
	{
		if(keyupInput($j("#building"+number), "Дом", 6, 1, "^[-a-zA-Zа-яА-ЯёЁ0-9 -\/.,]+$", 'Только русские и английские буквы и цифры и -/.,'))
		{
			$j("#checkAddressRes"+number).html('<img src="main_pics/ok.jpg" width="15" height="15">');
		}
		else $j("#checkAddressRes"+number).html('<img src="main_pics/nok.jpg" width="15" height="15">');
	});
	$j("#phoneInput"+number).keyup(function()
	{
		if(keyupInput($j("#phoneInput"+number), "Телефон", 20, 7, "^[-а-яА-Я0-9 -\/()]+$", 'Только русские буквы и цифры и -/(),'))
		{
			$j(".checkPhoneRes"+number).html('<img src="main_pics/ok.jpg" width="15" height="15">');
		}
		else if($j("#phoneInput"+number).val()=='')
		{
			$j(".checkPhoneRes"+number).html('');
		}
		else
		{
			$j(".checkPhoneRes"+number).html('<img src="main_pics/nok.jpg" width="15" height="15">');
		}
	});
}
function mallSuggest(number, city, street, building)
{
	$j.ajax(
		{
			url: "actions/suggest.php",
			data: "city="+city+"&s="+encodeURIComponent(street)+"&building="+encodeURIComponent(building)+"&number="+number,
			success: function(result)
			{
				$j("#mallSuggest"+number).html(result);
			}
		});
}
function addressSuggest()
{
	$j(".streetInput").keyup(function(event)
	{
		var number=$j(this).attr("id").substr(6, 3);
		var val=$j(this).val();
		var city=$j("#city").val();
		if(!$j("#city").val() || $j("#city").val()=='Выберите город')
		{
			alert('Сначала выберите город!');
			$j("#street"+number).val("");
			$j("#building"+number).val("");
		}
		$j("#building"+number).val('');
		$j("#buildingSuggest"+number).html('');
		$j("#mallSuggest"+number).html('');
		$j.ajax(
		{
			url: "actions/suggest.php",
			data: "city="+city+"&s="+encodeURIComponent(val),
			success: function(result)
			{
				$j("#streetSuggest"+number).html(result);
				$j(".streetResult").mouseover(function(event)
				{
					$j(this).css("background-color", "#8EE5EE");
				});
				$j(".streetResult").mouseout(function(event)
				{
					$j(this).css("background-color", "white");
				});
				$j(".streetResult").click(function(event)
				{
					var click=$j(this).html();

					$j("#streetSuggest"+number).slideUp(300,
					function()
					{
						$j("#street"+number).val(click);
						$j("#streetSuggest"+number).html('');
						$j("#streetSuggest"+number).css("display", "block");
					});
				});
			}
		});
	});
	$j(".buildingInput").keyup(function(event)
	{
		var number=$j(this).attr("id").substr(8, 3);
		$j("#mallSuggest"+number).html('');
		if($j("#street"+number).val().length<3)
		{
			alert('Сначала Введите улицу!');
			$j("#building"+number).val('')
			return;
		}
		var val=$j(this).val();
		var city=$j("#city").val();
		var street=$j("#street"+number).val();
		$j.ajax(
		{
			url: "actions/suggest.php",
			data: "city="+city+"&s="+encodeURIComponent(street)+"&b="+encodeURIComponent(val),
			success: function(result)
			{
				if(result=='')
				{
					$j("#buildingSuggest"+number).html('');
					mallSuggest(number, city, $j("#street"+number).val(), $j("#building"+number).val());
				}
				$j("#buildingSuggest"+number).html(result);
				$j(".buildingResult").mouseover(function(event)
				{
					$j(this).css("background-color", "#8EE5EE");
				});
				$j(".buildingResult").mouseout(function(event)
				{
					$j(this).css("background-color", "white");
				});
				$j("body:not(.buildingResult)").one("click", function()
				{
					$j("#buildingSuggest"+number).slideUp(300,
					function()
					{
						$j("#buildingSuggest"+number).html('');
						$j("#buildingSuggest"+number).css("display", "block");
					});
				});
				$j(".buildingResult").click(function(event)
				{
					var click=$j(this).html();
					$j("#buildingSuggest"+number).slideUp(300,
					function()
					{
						$j("#building"+number).val(click);
						$j("#buildingSuggest"+number).html('');
						$j("#buildingSuggest"+number).css("display", "block");
						mallSuggest(number, city, $j("#street"+number).val(), $j("#building"+number).val());
					});
				});
			}
		});
	});
}
function addAddressForm()
{
	$j(".addAddressForm").one("click", function(event)
	{
		var button=$j(this);
		var exNumber=$j(this).attr("id").substr(3);
		var number=parseInt($j(this).attr("id").substr(3))+1;
        var div=$j(this).parent();
        var parentdiv=$j(this).parent().parent();
		$j.ajax(
		{
			url: "actions/addAddressForm.php",
			data: "number="+number,
			success: function(result)
			{
				parentdiv.append(result);
				addressSuggest();
				addAddressForm();
				workdays();
				checkAddress(number);
				$j(".deleteAddressForm").one("click", function(event)
				{
					//$j(this).parent().html("");
					$j(this).parent().remove();
					$j(button).css("display", "inline-block");
				});
			}
		});
		$j(this).css("display", "none");
		$j("#daf"+exNumber).css("display", "none");
		//if(typeof checkAddress=='function')

	});
}